﻿
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Reflection;
using System.Collections.Generic;
using ZX.RuleEngine.Core;
using ScriptNET.Runtime;
using ZX.BLL.Extend;
using ZX.EHR.Library;
using ZX.DBModule;
using System.Collections;

namespace ZX.Test
{
    
    
    /// <summary>
    ///这是 RoleScriptTest 的测试类，旨在
    ///包含所有 RoleScriptTest 单元测试
    ///</summary>
    [TestClass()]
    public class RoleScriptTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 测试脚本
        [TestMethod]
        public void RunScript()
        {
            
            //([工资基数表.工资基数(前台)])-[薪资考勤月汇总表.事假小时数]*([工资基数表.工资基数(后台)]/21.75/8)





            hrEmployee employee = new hrEmployee();
            employee.CName = "江怀玉";
            string _script = @"msg='';
                               msg = entity.CName;
                               function check(e){
                                   if(e.CName =='')
                                   {
                                      return '姓名不能为空';
                                   }

                                   if(e.CName=='江怀玉')
                                   {
                                      return e.CName;
                                   }
                                   
                                   return '';
                                }
                               func = check;
                               msg = func(entity);
                               ";
            string msg = RoleRuntimeHost.CheckData(employee, _script);
            msg.Equals("江怀玉");

            //没有实体对象的时候
            DataTableBaseEntity obj = new DataTableBaseEntity();
            obj.TableName = "hrEmployee";
            obj.CreateConfigDT(obj.TableName);
            obj.CreateDT();
            obj.SetValue("CName", "jianghuaiyu");
            
            //obj["CName"] = "";
            string _sccript0 = @"msg='';
                               msg = entity['CName'];
                               function check(e){
                                   if(e['CName'].ToString() =='')
                                   {
                                      return '姓名不能为空';
                                   }

                                   if(e['CName'].ToString()=='江怀玉')
                                   {
                                      return e['CName'];
                                   }
                                   
                                   return e['CName'];
                                }
                               func = check;
                               msg = func(entity);
                               ";
            obj.SetValue("CName", "");
            msg = RoleRuntimeHost.CheckData(obj, _sccript0);

            //设值处理
            string _sccript1 = @"msg='';
                               msg = entity['CName'];
                               function check(e){
                                  e.SetValue('EName','jianghuaiyu');
                                  e.SetValue('CName','江怀玉');
                                  e.SetValue('IDCardNO','510722198812171773');
                                  return e['CName'];
                                }
                               func = check;
                               msg = func(entity);
                               ";
            //obj.SetValue("CName", "");
            msg = RoleRuntimeHost.CheckData(obj, _sccript1);
            //msg.Equals("江怀玉");

            obj["CName"].Equals("江怀玉");
            obj["EName"].Equals("jianghuaiyu");
        }
        [TestMethod]
        public void TestCalSalary()
        {
            //Guid employeeID = new Guid("");
            DAL.DBExtend.DbSql db = new DAL.DBExtend.DbSql();
            IList<DataTableBaseEntity> list = new List<DataTableBaseEntity>();
            
            Double? _v =  ZX.RuleEngine.Core.RoleRuntimeHost.CalData(list, "", "");
        }


        private void CalSalary(Guid employeeID, string YearMonth)
        {
            RuntimeHost.AddType("baseEntity", typeof(ZX.DBModule.BaseEntity));
            RuntimeHost.AddType("dtBaseEntity", typeof(ZX.DBModule.DataTableBaseEntity));
            RuntimeHost.AddType("dc", typeof(ZX.Common.Extend.DataCheck));
            RuntimeHost.AddType("bll", ZX.Common.Reflection.CreateInstance("ZX.BLL.Extend.BaseBussnies", "ZX.BLL").GetType());
            RuntimeHost.AddType("dal", typeof(ZX.DAL.DBExtend.DbSql));
            string _sccript1 = @"
                               sal= new dtBaseEntity();//定义一个工资对象
                               //获取数据源
                               function getDataSource(tableName,YearMonth,EmployeeID){
                                    _dtEntity = new dtBaseEntity();
                                    _dtEntity.TableName = tableName;
                                    _dtEntity.CreateDT();
                                    _bll = new bll(_dtEntity);
                                    //_bll.
                               }
                               ";

            //Script _script = RoleScript.Compile(script);
            //_script.Context.SetItem("entity", entity);
            //object rez = _script.Execute();
            //return rez.ToString();
        }

        private DataTableBaseEntity GetTimeDataSource(string tableName, string EmpCode,DateTime bdate,DateTime edate, bool IsData)
        {
            DataTableBaseEntity dtBE = new DataTableBaseEntity();
            dtBE.CreateDT();
            dtBE.TableName = tableName;
            DAL.DBExtend.DbSql db = new DAL.DBExtend.DbSql();
            System.Data.DataTable dt = db.ExecuteQuery("select * from EmpCode='{0} and workdate  between '{1}' and '{2}'");
            dtBE = new DataTableBaseEntity(dt);    
            return dtBE;
        }
        


        [TestMethod]
        public void TestBussines()
        {
            //ZX.BLL.Extend.BaseBussnies bll = new BaseBussnies();
            RuntimeHost.AddType("g", typeof(ZX.Common.Globals));
            //RuntimeHost.AddType("dc", typeof(ZX.Common.Extend.DataCheck));
            //RuntimeHost.AddType("bll", typeof(ZX.BLL.Extend.BaseBussnies));
            //RuntimeHost.AddType("dal", typeof(ZX.DAL.DBExtend.DbSql));
            var dt1 = System.DateTime.Now;
            hrEmployee employee = new hrEmployee();
            employee.CName = "江怀玉";
            string _script = @"msg='';
                               function check(e){
                                  e.CName='江怀玉2\'';   
                                  e.EName='RayJiang';
                                  e.IDCardNO = '1001001';
                                  e.DepartmentID = System.Guid.Parse('2AC0283A-B3C4-4AD0-BCF3-7C6CA1A5752D');
                                  e.AddTime = System.DateTime.Now;
                               }
                               check(entity);
                               _bll = new bll(entity);
                               _bll.Insert();
                               _dc = new dc();
                               if(_dc.IsEqual(0,0))
                               { 
                                 msg = msg+'\r\n'+'检验正确';
                               }
                               else
                               {
                                 msg = msg+'\r\n'+'检验正确';
                               }
                               ";

            string msg = RoleRuntimeHost.CheckData(employee, _script);
            ZX.BLL.Extend.BaseBussnies bll = new BaseBussnies(employee);
            employee = bll.GetEntityByCondition<hrEmployee>("IDCardNO='" + 1001001 + "'");
            msg.Equals("江怀玉");
            employee.CName.Equals("江怀玉2");
            var dt2 = System.DateTime.Now;
            var dtspan = (dt2 - dt1).Milliseconds;
            var dtspan1 = (dt2 - dt1).Seconds;
        }

        #endregion


        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

    }
}
